using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// 1. 添加 CORS 策略
builder.Services.AddCors(options => 
{
    options.AddPolicy("MyAllowSpecificOrigins", policy =>
    {
        policy.AllowAnyOrigin() // 允许的源
              .AllowAnyHeader()
              .AllowAnyMethod();
    });
});

builder.Services.AddDbContext<TodoDb>(opt => opt.UseInMemoryDatabase("TodoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(config =>
{
    config.DocumentName = "TodoAPI";
    config.Title = "TodoAPI v1";
    config.Version = "v1";
});

var app = builder.Build();

// 2. 启用 CORS 中间件（注意顺序！）
app.UseRouting();
app.UseCors("MyAllowSpecificOrigins"); // 全局启用 CORS
// app.UseAuthorization();

app.MapGet("/todoitems", async (TodoDb db) =>
    "现在的时间是："+DateTime.Now.ToString());

app.Run();